package com.topisv.tms.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.topisv.tms.entity.ExpressStyle;
import com.topisv.tms.entity.LogisticsCompany;
import com.topisv.tms.entity.QueryParamHolder;
import com.topisv.tms.repository.ExpressStyleDao;
import com.topisv.tms.repository.LogisticsCompanyDao;
import com.topisv.tms.service.base.BaseService;

//Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional(readOnly = true)
public class LogisticsCompanyService extends BaseService{
private LogisticsCompanyDao logisticsCompanyDao;
	
	@Autowired
	private ExpressStyleDao expressStyleDao;
	
	@Autowired
	public void setAreaGroupDao(LogisticsCompanyDao logisticsCompanyDao) {
		this.logisticsCompanyDao = logisticsCompanyDao;
	}
	public LogisticsCompany getLogisticsCompany(Long id) {
		if (id == null) {
			throw new IllegalArgumentException("id=" + id);
		}
		return logisticsCompanyDao.findOne(id);
	}
	
	@Transactional(readOnly = false)
	public void deleteLogisticsCompany(Long id) {
		if (id == null) {
			throw new IllegalArgumentException("id=" + id);
		}
		logisticsCompanyDao.delete(id);
	}
	
	@Transactional(readOnly = false)
	public LogisticsCompany addLogisticsCompany(LogisticsCompany arg0){
		if (arg0 == null) {
			throw new IllegalArgumentException("arg0=" + arg0);
		}
		return logisticsCompanyDao.save(arg0);
	}
	
	@Transactional(readOnly = false)
	public Iterable<LogisticsCompany> addLogisticsCompanys(List<LogisticsCompany> arg0){
		if (arg0 == null) {
			throw new IllegalArgumentException("arg0=" + arg0);
		}
		return logisticsCompanyDao.save(arg0);
	}
	
	public Page<LogisticsCompany> getLogisticsCompany(QueryParamHolder paramHolder) {
		Page<LogisticsCompany> logisticsCompanys = findByJson(paramHolder, logisticsCompanyDao, LogisticsCompany.class);
		List<LogisticsCompany> lists = logisticsCompanys.getContent();
		for (int i = 0; i < lists.size(); i++) {
			List<ExpressStyle> expressStyles = expressStyleDao.findStyleNames(lists.get(i).getId());
			lists.get(i).setExpressStyleSize(expressStyles.size());
		}
		return logisticsCompanys;
	}
	
	public List<LogisticsCompany> findAll(){
		return logisticsCompanyDao.findAll();
	}
	
	public LogisticsCompany getLogisticsCompanyByCode(String companyCode) {
		return this.logisticsCompanyDao.findCompanyByCode(companyCode);
	}
	
	public List<LogisticsCompany> findCompanyNameAndCode(){
		return logisticsCompanyDao.findCompanyNameAndCode();
	}
	
}
